package conchmusic.conchmusicclient.util;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import conchmusic.conchmusicclient.pojo.Lyric;

/**
 * Created by sbml1 on 2017/4/22.
 */

public class LrcUtils {


    /*public List<Lyric> getLrcByUrl(InputStream in){
        String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/lrc/weiwei.lrc";
        File file=new File(path);
        BufferedReader bufferedReader;
        String temp="";
        List<Lyric> lyrics=new ArrayList<>();

        try {
            bufferedReader=new BufferedReader(new InputStreamReader(in));
            String lrc=bufferedReader.readLine();
            String linetag="\\\\n";
            String lines[]=lrc.split(linetag);
            for(int i=0;i<lines.length;i++){
                temp=lines[i];
                //Log.i("temp",i+".........."+temp);
                List<Lyric> lyrics1=processLrc(temp);
                for(i=0;i<lyrics1.size();i++){
                    if(lyrics1.get(i)!=null){
                        lyrics.add(lyrics1.get(i));
                    }
                }
            }
            bufferedReader.close();
        } catch(Exception e){
            e.printStackTrace();
        }
        return lyrics;
    }*/

    public List<Lyric> getLrcByPath(String path){
        File file= null;
        try {
            file = new File(path);
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }

        InputStream inputStream=null;
        BufferedReader bufferedReader;
        String temp="";
        List<Lyric> lyrics=new ArrayList<>();
        try {
            inputStream=new BufferedInputStream(new FileInputStream(file));
            bufferedReader=new BufferedReader(new InputStreamReader(inputStream,getCharset(file)));
            String linetag="\\\\n";
            String lrc=bufferedReader.readLine();
            String lines[]=lrc.split(linetag);
            for(int i=0;i<lines.length;i++){
                temp=lines[i];
                //Log.i("temp",i+".........."+temp);
                List<Lyric> lyrics1=processLrc(temp);
                if(lyrics1!=null){
                    //Log.i("util size",lyrics1.size()+"...");
                    for(int j=0;j<lyrics1.size();j++){
                        if(lyrics1.get(j)!=null){
                            lyrics.add(lyrics1.get(j));
                        }
                    }
                }

            }

            bufferedReader.close();
            inputStream.close();
        } catch(Exception e){
            e.printStackTrace();
            return null;
        }
        //对歌词按时间戳排序再返回
        //Collections.sort(lyrics,new Sort());
        return lyrics;
    }

    //处理一行
    private List<Lyric> processLrc(String text){
        List<Lyric> lyrics=new ArrayList<>();
        String lineTemp=text;
        try{
            int pos1=text.indexOf("[");
            int pos2=text.indexOf("]");

            if(pos1>=0 && pos2!=-1){
                Long time[] = new Long[getPossiblyTagCount(text)];
                time[0]=timeToLong(text.substring(pos1+1,pos2));
                if(time[0]==-1){
                    return null;
                }
                int i = 1;
                while (pos1 >= 0 && pos2 != -1) {

                    lineTemp = lineTemp.substring(pos2 + 1);
                    pos1 = lineTemp.indexOf("[");
                    pos2 = lineTemp.indexOf("]");
                    if (pos2 != -1) {
                        time[i] = timeToLong(lineTemp.substring(pos1 + 1, pos2));
                        if (time[i] == -1)
                            break;
                        i++;
                    }
                }
                for (int j = 0; j < time.length; j++) {
                    if (time[j] != null) {
                        Lyric lyric = new Lyric();
                        lyric.timePint = time[j].intValue();
                        lyric.lricString = lineTemp;
                        lyrics.add(lyric);
                    }
                }
                return lyrics;
            }else {
                return null;
            }
        }catch(Exception e){
            return null;
        }

    }

    //获取一行中的时间标签的个数，为了防止将重复歌词放在一行上显示
    private static int getPossiblyTagCount(String Line) {
        String strCount1[] = Line.split("\\[");
        String strCount2[] = Line.split("]");
        if (strCount1.length == 0 && strCount2.length == 0)
            return 1;
        else if (strCount1.length > strCount2.length)
            return strCount1.length;
        else
            return strCount2.length;
    }

    //转换时间
    private long timeToLong(String time){
        try{
            String[] s1=time.split(":");
            int min= Integer.parseInt(s1[0]);
            String[] s2 = s1[1].split("\\.");
            int sec = Integer.parseInt(s2[0]);
            int mill = 0;
            if (s2.length > 1)
                mill = Integer.parseInt(s2[1]);
            long TimeLong=min*60*1000+sec*1000+mill*10;
            return TimeLong;
        }catch (Exception e){
            return -1;
        }
    }

    /*private static class Sort implements Comparator<Lyric> {
        public Sort() {
        }

        public int compare(Lyric tl1, Lyric tl2) {
            return sortUp(tl1, tl2);
        }

        private int sortUp(Lyric tl1, Lyric tl2) {
            if (tl1.timePint < tl2.timePint)
                return -1;
            else if (tl1.timePint > tl2.timePint)
                return 1;
            else
                return 0;
        }

    }*/

    //获取歌词文件编码方式
    public String getCharset(File file){
        return "UTF8";
    }

    /*public static String getCharset(File file) {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false;
            BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(file));
            bis.mark(0);
            //一般读取前3个字节就可以判断文件的编码格式
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1)
                return charset;
            if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xFE
                    && first3Bytes[1] == (byte) 0xFF) {
                charset = "UTF-16BE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xEF
                    && first3Bytes[1] == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8";
                checked = true;
            }
            bis.reset();
            if (!checked) {
                int loc = 0;
                while ((read = bis.read()) != -1) {
                    loc++;
                    if (read >= 0xF0)
                        break;
                    if (0x80 <= read && read <= 0xBF)
                        break;
                    if (0xC0 <= read && read <= 0xDF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF)
                            continue;
                        else
                            break;
                    } else if (0xE0 <= read && read <= 0xEF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) {
                                charset = "UTF-8";
                                break;
                            } else
                                break;
                        } else
                            break;
                    }
                }
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return charset;
    }*/



}
